package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    DtsBrandMapper brandMapper;
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;



    @Override
    public List<Integer> queryById(Integer id) {
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getAdminId,id);
        qw.select(DtsBrand::getId);
        List<DtsBrand> dtsBrands = brandMapper.selectList(qw);
        List<Integer> ids = dtsBrands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());
        return ids;
    }

    @Override
    public List<Map<String, Object>> findBrand() {
        LambdaQueryWrapper<DtsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getDeleted, false);
        List<DtsBrand> brands = brandMapper.selectList(qw);
        List<Map<String, Object>> list = brands.stream().map(brand -> {

            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label", brand.getName());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public IPage<DtsBrand> queryPage(Integer page, Integer limit, String sort, String order, String username, String id) {
        IPage<DtsBrand> ipage = new Page(page, limit);
        QueryWrapper<DtsBrand> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            qw.like("username", username);
        }
        if(!StringUtils.isEmpty(id)) {
            qw.like("id", id);
        }
        qw.eq("deleted",0);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            } else {
                qw.orderByAsc(sort);
            }
        }
        brandMapper.selectPage(ipage,qw);

        return ipage;
    }

    @Override
    public DtsBrand create(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
         brandMapper.insert(brand);
         return brand;
    }

    @Override
    public DtsBrand update(DtsBrand brand) {
        LambdaQueryWrapper<DtsBrand> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsBrand::getName,brand.getName());
        Integer i = brandMapper.selectCount(qw);
        if(i>0){
            ResponseUtil.fail(-1,"名称已存在");
        }

        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
        return brand;
    }

    @Override
    public void delete(DtsBrand brand) {
        Integer id = brand.getId();
        LambdaQueryWrapper<DtsGoods> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getBrandId,id);
        List<DtsGoods> dtsGoods = goodsMapper.selectList(qw);
        if(dtsGoods!=null&&dtsGoods.size()>0){
            for (DtsGoods dtsGood : dtsGoods) {
                dtsGood.setDeleted(true);
                goodsMapper.updateById(dtsGood);
            }
        }
        LambdaQueryWrapper<DtsBrand> qw2=new LambdaQueryWrapper<>();
        brand.setDeleted(true);
        qw2.eq(DtsBrand::getId,id);
        brandMapper.updateById(brand);
    }
}
